package gui;

import javax.swing.*;

import mapData.Coord;
import mapData.Map;
import mapData.Obstruction;
import mapData.UnitRow;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/*
 * main class for running game
 */
public class rts
{
    public static void main(String[] args) {
	    SwingUtilities.invokeLater(new Runnable() {
	        public void run() {
	            createAndShowGUI();
	        }
	    });
        
    }
    
    
    private static void createAndShowGUI() {
    	System.out.println("Created GUI on EDT? "+
                           SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("RTS");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
		try
		{
			String mapName;
			Scanner ui = new Scanner(System.in);
			System.out.print("Input map name: ");
			mapName = ui.next();
			
			
			Map gameMap = readToMap("maps/"+mapName);
			System.out.println("read from file: " +mapName +" ... successful"); 
		    int width = gameMap.getWidth();
		    int height = gameMap.getHeight();
		    System.out.println("map has "+width+" tiles across");
		    System.out.println("map has "+height+" tiles down");
		    f.add(new GamePanel(gameMap));
		    f.setTitle("RTS Map: "+gameMap.name);
		    
		    f.pack();
		    
		    //f.setSize((width)*5+19,(height)*5);
		    f.setResizable(false);
		    ImageIcon i = new ImageIcon("src/gui/guy_final copy.jpg");
		    Image j = i.getImage();
		    f.setIconImage(j);
		    f.setVisible(true);
		}
        catch(IOException e)
        {
            System.out.println("read from file failed");
            System.exit(1);
        }
        
        
    }
    //reads map data and returns map class
    
    private static Map readToMap(String mapName) throws IOException
    {
        String name;
        int width;
        int height;
        
        Color plyr;
        Color cmp;
        Scanner sc = new Scanner(new FileReader(mapName));
        
        name = sc.next();
        width = sc.nextInt();
        height = sc.nextInt();
        plyr = getCol(sc.next()); 
        cmp = getCol(sc.next());
        Map out = new Map(name, width, height, plyr, cmp);
        
        int numObstr = sc.nextInt();
        for(int i = 0; i < numObstr; i++)
        {
            int topX = sc.nextInt();
            int topY = sc.nextInt();
            int rectW = sc.nextInt();
            int rectH = sc.nextInt();
            Obstruction obstrAdd = new Obstruction( topX, topY, rectW, rectH);
            out.obstrList.add(obstrAdd);
        }
        
//      [Unit ID# (0 or 1)] [x] [y] [# in row] [horizontal spacing] [plyr/comp]
//      public UnitRow(int ID, Coord lm, int size, int spcng, Color col)
        int numRows = sc.nextInt();
        for(int i = 0; i < numRows; i++)
        {
            int ID = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            int size = sc.nextInt();
            int spacing = sc.nextInt();
            String side = sc.next();
            Color unitCol;
            if(side.equals("plyr"))
            {
                unitCol = plyr;
                UnitRow addRow = new UnitRow(ID, new Coord(x,y), size, spacing, unitCol);
                out.plyrRowList.add(addRow);
            }
            else if(side.equals("comp"))
            {
                unitCol = cmp;
                UnitRow addRow = new UnitRow(ID, new Coord(x,y), size, spacing, unitCol);
                out.cmpRowList.add(addRow);
            }
            else
            {
                System.out.println("no such side");
                System.exit(1);
            } 
        }
        System.out.println(out.plyrRowList.size() +" player Unit row(s) added");
        System.out.println(out.cmpRowList.size() +" computer Unit row(s) added");
        return out;
        
    }
    
    private static Color getCol(String colTemp)
    {
        //System.out.println(colTemp);
        Color out = new Color(0,0,0);
        if(colTemp.equals("red"))
        {
            out = new Color(255, 0, 0);
        }
        else if(colTemp.equals("blue"))
        {
            out = new Color(0, 0, 255);
        }
        else if(colTemp.equals("yellow"))
        {
            out = new Color(255, 255, 0);
        }
        else if(colTemp.equals("white"))
        {
            out = new Color(255, 255, 255);
        }
        else
        {
            System.out.println("non-valid color\n");
            System.exit(1);
        }
        return out;
    }	
}